// 生成验证码
function createCode(codeID) {
    var code = "";
    // 验证码长度
    var codeLength = 4;
    // 验证码dom元素
    var checkCode = $(codeID);
    // 验证码随机数
    var random = [0,1,2,3,4,5,6,7,8,9,'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R',
        'S','T','U','V','W','X','Y','Z'];
    for (var i = 0;i < codeLength; i++){
        // 随机数索引
        var index = Math.floor(Math.random()*36);
        code += random[index];
    }
    // 将生成的随机验证码赋值
    checkCode.val(code);
}
// 校验验证码、用户名、密码
function validateCode(inputID,codeID) {
    var inputCode = $(inputID).val().toUpperCase();
    var cardCode = $(codeID).val();
    var loginUsername = $('#loginUsername').val();
    var loginPassword = $('#loginPassword').val();
    if ($.trim(loginUsername) == '' || $.trim(loginUsername).length<=0){
        layer.alert("用户名不能为空");
        return false;
    }
    if ($.trim(loginPassword) == '' || $.trim(loginPassword).length<=0){
        layer.alert("密码不能为空");
        return false;
    }
    if (inputCode.length<=0){
        layer.alert("验证码不能为空");
        return false;
    }
    if (inputCode != cardCode){
        layer.alert("请输入正确验证码");
        return false;
    }
    return true;
}
// 登录流程
function login() {
    if (!validateCode('#loginCard','#loginCode')){
        //阻断提示
    }else {
        var loginUsername = $('#loginUsername').val();
        var loginPassword = $('#loginPassword').val();
        var params = {};
        params.loginUsername = loginUsername;
        params.loginPassword = loginPassword;
        var loginLoadIndex = layer.load(2);
        $('#loginBtn').val("正在登录...");
        $.ajax({
            type:'post',
            url:'user/login',
            dataType:'json',
            data:JSON.stringify(params),
            contentType:'application/json',
            success:function (result) {
                layer.close(loginLoadIndex);
                if (result.state = 1){
                    window.location.href = 'page/index';
                }
            },
            error:function () {
                popMessage("登陆错误");
            }
        });
    }

}
// 注册流程
function register() {
    var register = "<div class=\"registerPage\">\n" +
        "    <div class=\"registerDiv\">\n" +
        "        <form>\n" +
        "            <div class=\"usernameWrapDiv\">\n" +
        "                <div class=\"usernameLabel\">\n" +
        "                    <label>用户名:</label>\n" +
        "                </div>\n" +
        "                <div class=\"usernameDiv\">\n" +
        "                    <i class=\"layui-icon layui-icon-username adminIcon\"></i>\n" +
        "                    <input id=\"registerUsername\" class=\"layui-input adminInput\" type=\"text\" name=\"username\" placeholder=\"输入用户名\" >\n" +
        "                </div>\n" +
        "            </div>\n" +
        "            <div class=\"usernameWrapDiv\">\n" +
        "                <div class=\"usernameLabel\">\n" +
        "                    <label>密码:</label>\n" +
        "                </div>\n" +
        "                <div class=\"passwordDiv\">\n" +
        "                    <i class=\"layui-icon layui-icon-password adminIcon\"></i>\n" +
        "                    <input id=\"registerPassword\" class=\"layui-input adminInput\" type=\"password\" name=\"password\" placeholder=\"输入密码\">\n" +
        "                </div>\n" +
        "            </div>\n" +
        "            <div class=\"usernameWrapDiv\">\n" +
        "                <div class=\"usernameLabel\">\n" +
        "                    <label>确认密码:</label>\n" +
        "                </div>\n" +
        "                <div class=\"passwordDiv\">\n" +
        "                    <i class=\"layui-icon layui-icon-password adminIcon\"></i>\n" +
        "                    <input id=\"registerWellPassword\" class=\"layui-input adminInput\" type=\"password\" name=\"password\" placeholder=\"输入密码\">\n" +
        "                </div>\n" +
        "            </div>\n" +
        "            <div class=\"usernameWrapDiv\">\n" +
        "                <div class=\"usernameLabel\">\n" +
        "                    <label>所在城市</label>\n" +
        "                </div>\n" +
        "                <div class=\"passwordDiv\">\n" +
        "                    <select id=\"areaSelect\" class=\"layui-select\">\n" +
        "                        <option value=\"\">请选择...</option>\n" +
        "                        <option value=\"成都市\">成都市</option>\n" +
        "                    </select>\n" +
        "                </div>\n" +
        "            </div>\n" +
        "            <div class=\"usernameWrapDiv\">\n" +
        "                <div class=\"usernameLabel\">\n" +
        "                    <label>邮箱地址</label>\n" +
        "                </div>\n" +
        "                <div class=\"passwordDiv\">\n" +
        "                     <i class=\"layui-icon layui-icon-password adminIcon\"></i>\n" +
        "                    <input id=\"registerEmail\" class=\"layui-input adminInput\" type=\"email\" name=\"email\" placeholder=\"邮箱地址\">\n" +
        "                    <input id=\"registerCode\" class=\"layui-input adminInput\" type=\"text\" placeholder='邮箱验证码'>\n" +
        "                    <input id=\"sendEmailCodeBtn\" class=\"layui-input adminInput\" type=\"button\" value='获取邮箱验证码'>\n" +
        "                </div>\n" +
        "            </div>\n" +
        "<div class=\"usernameWrapDiv\">\n" +
    "                <div class=\"usernameLabel\">\n" +
    "                    <label>所在地</label>\n" +
    "                </div>\n" +
    "                <div class=\"passwordDiv\">\n" +
    "                    <select id=\"citySelect\" class=\"layui-select\">\n" +
    "                        <option value=\"\">请选择...</option>\n" +
    "                        <option value=\"双流区\">双流区</option>\n" +
    "                    </select>\n" +
    "                </div>\n" +
    "            </div>\n" +
        "        </form>\n" +
        "    </div>\n" +
        "</div>\n"
    layer.open({
        type:'1',
        content:register,
        title:'注册',
        area:['430px','500px'],
        btn:['注册','重置','取消'],
        closeBtn:'1',
        yes:function (index,layero) {
            //注册回调
            var registerUsername = $('#registerUsername').val();
            var registerPassword = $('#registerPassword').val();
            var registerWellPassword = $('#registerWellPassword').val();
            var area = $('#areaSelect option:selected').val();
            var city = $('#citySelect option:selected').val();
            var params = {};
            params.registerUsername = registerUsername;
            params.registerPassword = registerPassword;
            params.registerWellPassword = registerWellPassword;
            params.area = area;
            params.city = city;
            var registerLoadIndex = layer.load(2);
            $.ajax({
                type:'post',
                url:"user/register",
                dataType:'json',
                data:JSON.stringify(params),
                contentType:'application/json',
                success:function (result) {
                    layer.msg(result.data.message);
                },
                error:function () {
                    layer.msg("服务器未响应")
                }
            });
        },
        btn2:function (index,layero) {
            //重置回调
            var registerUsername = $('#registerUsername').val("");
            var registerPassword = $('#registerPassword').val("");
            var registerWellPassword = $('#registerWellPassword').val("");
            // 防止注册页面关闭
            return false;
        },
        btn3:function (index,layero) {
            //取消回调
        }
    })
}
